<template>
  <view>
    <u-navbar :border-bottom="false" :custom-back="onBack">
      <view class="search-input"
        ><u-search
          v-model="keyword"
          height="72"
          placeholder="输入机构名称"
          @custom="handleSearch"
        ></u-search
      ></view>
    </u-navbar>
    <view class="list">
      <view
        class="item u-border-bottom"
        v-for="item in schollList"
        :key="item.id"
        @click="handleSwitch(item.id)"
        >{{ item.title }}</view
      >
      <edu-empty v-if="$u.test.empty(schollList)"></edu-empty>
    </view>
  </view>
</template>

<script>
import { mapState, mapGetters } from 'vuex'
import { storage } from '@/utils/storage'
export default {
  data() {
    return {
      keyword: '',
      schollList: [],
    }
  },
  computed: {
    ...mapState({
      schollSearchHistoryRecordList: (state) => state.school.searchHistoryRecord,
      globSchoolId: (state) => state.projectSetting.globSchoolId,
    }),
    ...mapGetters({
      hasLogin: 'user/getHasLogin',
    }),
  },
  // 监听页面返回
  onBackPress(options) {
    if (options.from === 'navigateBack') {
      return false
    }
    this.onBack()
    return true
  },
  onLoad(options) {
    if (!this.$u.test.empty(options)) {
      this.keyword = options.title
      this.getSchollData()
    }
  },
  methods: {
    getSchollData() {
      this.$edu.api
        .getSchoolSearch({
          title: this.keyword,
        })
        .then((res) => {
          const { data } = res
          this.schollList = data
          this.addHistory(this.keyword)
        })
    },
    // 搜索列表
    handleSearch() {
      this.getSchollData()
    },
    // 添加搜索历史记录
    addHistory(val) {
      var newArr = this.schollSearchHistoryRecordList.filter((item) => item == val)
      if (val == '' || newArr.length > 0) {
        return
      }
      let list = this.schollSearchHistoryRecordList
      list.unshift(val)
      this.$store.commit('school/setSearchHistoryRecord', list)
    },
    handleSwitch(id) {
      if (this.globSchoolId != id) {
        if (this.hasLogin) {
          this.$store.dispatch('user/logout')
        }
        this.$store.commit('projectSetting/setSchoolId', id)
        storage.set('edu-home-module-nav-status', 0)
        storage.set('edu-home-module-nav-list', [])
      }
      this.$u.route({
        type: 'reLaunch',
        url: '/pages/index/index',
      })
    },
    // 返回上一页
    onBack() {
      let pages = getCurrentPages()
      let page = pages[pages.length - 2]
      if (this.$u.test.empty(page)) {
        this.$u.route({ type: 'switchTab', url: 'pages/index/index' })
      } else {
        this.$u.route({ type: 'back' })
      }
    },
  },
}
</script>

<style lang="scss" scoped>
.search-input {
  width: 100%;
  padding-right: 20rpx;
}
.list {
  min-height: calc(100vh - 44px);
  background-color: #fff;
  padding: 30rpx;
  .item {
    font-size: 28rpx;
    font-weight: 400;
    color: $edu-content-color;
    padding: 26rpx 0;
  }
}
</style>
